package org.dingfugui.triple.protocol.binary.aio;

import org.dingfugui.triple.connection.ConnectionFactory;
import org.dingfugui.triple.context.producer.ExecuteHandler;
import org.dingfugui.triple.protocol.Acceptor;
import org.dingfugui.triple.protocol.Protocol;
import org.dingfugui.triple.protocol.RequestHandler;
import org.dingfugui.triple.protocol.Translator;
import org.dingfugui.triple.protocol.mssage.DefaultTranslator;
import org.dingfugui.triple.serialize.jdk.JdkSerialize;

/**
 * Created by fugui.ding on 2015/6/10.
 */
public class AioProtocol implements Protocol {

    private Acceptor acceptor;
    private ConnectionFactory connectionFactory;
    private Translator translator = new DefaultTranslator(new JdkSerialize());

    public AioProtocol(ExecuteHandler executeHandler) {
        acceptor = new AioAcceptor(new RequestHandler(new DefaultTranslator(), executeHandler));
    }

    public AioProtocol(int port, ExecuteHandler executeHandler) {
        acceptor = new AioAcceptor(port, new RequestHandler(new DefaultTranslator(), executeHandler));
    }

    public AioProtocol(Acceptor acceptor, ConnectionFactory connectionFactory, Translator translator) {
        this.acceptor = acceptor;
        this.connectionFactory = connectionFactory;
        this.translator = translator;
    }

    public AioProtocol(String host) {
        this.connectionFactory = new AioConnectionFactory();
    }

    public AioProtocol(String host, int port) {
        this.connectionFactory = new AioConnectionFactory();
    }

    @Override
    public Acceptor getAcceptor() {
        return acceptor;
    }

    @Override
    public ConnectionFactory getConnectionFactory() {
        return connectionFactory;
    }

    @Override
    public Translator getTranslator() {
        return translator;
    }

    public void setAcceptor(Acceptor acceptor) {
        this.acceptor = acceptor;
    }

    public void setConnectionFactory(ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    public void setTranslator(Translator translator) {
        this.translator = translator;
    }
}
